import http from '@/utils/http'

const BASE_URL = '/system/job'

export interface ScheduleJobResp {
  id: string
  jobKey: string
  jobName: string
  cron: string
  paramsJson: string
  initStrategy: string
  saveLog: string
  description: string
  status: string
  createUser: string
  createTime: string
  updateUser: string
  updateTime: string
}

export interface ScheduleJobDetailResp extends ScheduleJobResp {
}

export interface ScheduleJobQuery extends SortQuery {
  jobName?: string
  status?: string
}

export interface ScheduleJobPageQuery extends ScheduleJobQuery, PageQuery {

}

export interface ScheduleJobLogResp {
  id: string
  jobId: string
  jobName: string
  cron: string
  paramJson: string
  startTime: string
  endTime: string
  elapsedSeconds: string
  runStatus: string
  dataCount: string
  resultMessage: string
}

export interface ScheduleJobLogDetailResp extends ScheduleJobLogResp {
}

export interface ScheduleJobLogQuery extends SortQuery {
  jobName?: string
  runStatus?: string
}

export interface ScheduleJobLogPageQuery extends ScheduleJobLogQuery, PageQuery {

}

/** @desc 查询定时任务列表 */
export function getJobBeanList() {
  return http.get<any[]>(`${BASE_URL}/list/job-beans`)
}

/** @desc 查询定时任务列表 */
export function getPage(query: ScheduleJobPageQuery) {
  return http.get<Page<ScheduleJobResp[]>>(`${BASE_URL}/page`, query)
}

/** @desc 查询定时任务详情 */
export function getDetail(id: string) {
  return http.get<ScheduleJobDetailResp>(`${BASE_URL}/detail/${id}`)
}

/** @desc 创建定时任务 */
export function create(data: any) {
  return http.post(`${BASE_URL}/create`, data)
}

/** @desc 修改定时任务 */
export function update(data: any, id: string) {
  return http.post(`${BASE_URL}/update/${id}`, data)
}

/** @desc 删除定时任务 */
export function remove(ids: string | Array<string>) {
  return http.post(`${BASE_URL}/delete/${ids}`)
}

/** @desc 查询定时任务日志列表 */
export function getLogPage(query: ScheduleJobLogPageQuery) {
  return http.get<Page<ScheduleJobLogResp[]>>(`${BASE_URL}/log/page`, query)
}

/** @desc 查询定时任务日志详情 */
export function getLogDetail(id: string) {
  return http.get<ScheduleJobLogDetailResp>(`${BASE_URL}/log/detail/${id}`)
}

/** 立即执行一次 */
export function executeOnce(id: string) {
  return http.post(`${BASE_URL}/execute/${id}`)
}
